﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AbstraX;
using System.Reflection;

namespace SolutionFramework.Controls
{
    public delegate void ShowInnerExceptionHandler(Exception exception, RemotableException remoteException);
    public delegate void ViewLogHandler();

    public partial class ExceptionControl : UserControl
    {
        private Exception exception;
        private RemotableException remoteException;
        public event ShowInnerExceptionHandler ShowInnerException;
        public event ViewLogHandler ViewLog;

        public Exception Exception
        {
            get 
            { 
                return exception; 
            }
            
            set 
            { 
                exception = value;
                MessageText.Text = exception.Message;
                SourceText.Text = Assembly.GetExecutingAssembly().ManifestModule.Name;
                StackTraceText.Text = exception.StackTrace;

                if (exception.InnerException == null)
                {
                    cmdInnerException.IsEnabled = false;
                }
            }
        }

        public RemotableException RemoteException
        {
            get 
            { 
                return remoteException; 
            }
            
            set 
            { 
                remoteException = value;

                MessageText.Text = remoteException.Message ?? string.Empty;
                SourceText.Text = remoteException.Source ?? string.Empty;
                StackTraceText.Text = remoteException.StackTrace ?? string.Empty;

                if (remoteException.InnerException == null)
                {
                    cmdInnerException.IsEnabled = false;
                }
            }
        }

        public ExceptionControl()
        {
            InitializeComponent();
        }

        private void cmdInnerException_Click(object sender, RoutedEventArgs e)
        {
            ShowInnerException(exception != null ? exception.InnerException : null, remoteException != null ? remoteException.InnerException : null);
        }

        private void cmdViewLog_Click(object sender, RoutedEventArgs e)
        {
            ViewLog();
        }
    }
}
